package com.example.androidcomplete0102.ch19;

import com.example.androidcomplete0102.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class LongTime3 extends Activity {
	int mValue;
    TextView mText;
    ProgressDialog mProgress;
    boolean mQuit;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ch19_uploadex);
        mText=(TextView)findViewById(R.id.ch19_text);
    }
    
    public void mOnClick(View v) {
        if (mValue ==100) mValue = 0;

        mProgress =new ProgressDialog(this);
        mProgress.setTitle("Updating");
        mProgress.setMessage("Wait...");
        mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//      mProgress.setCancelable(true);
        mProgress.setOnCancelListener(new OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                mQuit = true;
                //mProgress.dismiss();                
            }
        });
        mProgress.show();
        mQuit = false;
        mHandler.sendEmptyMessage(0);
    }

    
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            mValue++;
            mText.setText(Integer.toString(mValue));
            try { Thread.sleep(50); } 
            catch (InterruptedException e) {;}
            if (mValue < 100 && mQuit == false) {
                mProgress.setProgress(mValue);
                mHandler.sendEmptyMessage(0);
            } else {
                mProgress.dismiss();
            }
        }
    };

}
